home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / CLOCK.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  6KB  |  201 lines

  1. /* CLOCK.H:  prototypes/defines for the CLOCK.C file                    */
  2. /*
  3.     This file written in 1990 Jonathan R. Guthrie and placed in the
  4.     public domain
  5. */
  6.  
  7. int     far     startclock(int x, int y, int attr);
  8. void    far     stopclock(void);
  9.  
  10. #define CLOCK_OK        0   /* Clock was installed properly             */
  11. #define CLOCK_ERR_INST  -1  /* Clock is already installed               */
  12. #define CLOCK_ERR_OOS   -2  /* Clock would be off the screen            */
  13.  
  14.  
  15. /* CLOCK.C:  An on-screen clock generator                               */
  16. /*
  17.     This file written in 1990 Jonathan R. Guthrie and placed in the
  18.     public domain
  19. */
  20.  
  21. #include    <stdlib.h>
  22. #include    <conio.h>
  23. #include    <time.h>
  24. #include    <dos.h>
  25. #include    "clock.h"
  26.  
  27. #define CLOCK       0x1c
  28. #define TRUE        1
  29. #define FALSE       0
  30.  
  31. /* Now, the stuff needed for the for the on-screen clock                */
  32.  
  33. static  void    interrupt   (*oldvector)(void);
  34. static  void    interrupt   do_clock(void);
  35. static  void    setstr(char *, int, size_t);
  36.  
  37. static  char    timestuff[22], hours, oddfives, ticks, ticklimit;
  38.  
  39. static  int     clockx, clocky, clockattr, installed = FALSE;
  40.  
  41. int     far     startclock(int x, int y, int attr)
  42. {
  43.       time_t  temptime;
  44.       struct  tm  *struct_time;
  45.       int     bigx;
  46.       struct  text_info   r;
  47.  
  48.       /* The clock starting routine */
  49.  
  50.       /* First, see if it's already installed */
  51.  
  52.       if(installed)
  53.             return  CLOCK_ERR_INST;
  54.       else  installed = TRUE;
  55.  
  56.       /* Now, set assorted important module constants */
  57.  
  58.       clockx = x;
  59.       clocky = y;
  60.       clockattr = attr;
  61.  
  62.       gettextinfo(&r);
  63.  
  64.       if((clockx < 0) || (clockx > r.screenwidth - 10) ||
  65.          (clocky < 0) || (clocky > r.screenheight))
  66.       {
  67.             installed = FALSE;
  68.             return CLOCK_ERR_OOS;
  69.       }
  70.  
  71.       /* Now, set the program's clock */
  72.  
  73.       setstr(timestuff, clockattr, 22);
  74.  
  75.       time(&temptime);
  76.       struct_time = localtime(&temptime);
  77.  
  78.       oddfives    = 0;
  79.       ticklimit   = 17;
  80.       hours         = (struct_time->tm_hour + 11) % 12 + 1;
  81.       timestuff[0]  = (hours > 9) ? '1' : ' ';
  82.       timestuff[2]  = '0' + hours % 10;
  83.       timestuff[4]  = ':';
  84.       timestuff[6]  = '0' + struct_time->tm_min / 10;
  85.       timestuff[8]  = '0' + struct_time->tm_min % 10;
  86.       timestuff[10] = ':';
  87.       timestuff[12] = '0' + struct_time->tm_sec / 10;
  88.       timestuff[14] = '0' + struct_time->tm_sec % 10;
  89.       timestuff[16] = ' ';
  90.       timestuff[18] = (struct_time->tm_hour > 11) ? 'P' : 'A';
  91.       timestuff[20] = 'M';
  92.  
  93.       /* Now, initialize the clock as displayed on the screen */
  94.  
  95.       puttext(clockx, clocky, clockx+10, clocky, timestuff);
  96.  
  97.       /* Finally, set the vector to point to the clock routine */
  98.  
  99.       disable();
  100.       oldvector = getvect(CLOCK);
  101.       setvect(CLOCK, do_clock);
  102.       enable();
  103.       return  CLOCK_OK;
  104. }
  105.  
  106. static  void    interrupt   do_clock(void)
  107. {
  108.       ++ticks;
  109.       if(ticks > ticklimit)   /* Then, it's time to update the seconds */
  110.       {
  111.             ticks = 0;
  112.  
  113.             /* First, handle the fractional Hz part */
  114.  
  115.             ++oddfives;
  116.             if (5 == oddfives)
  117.             {
  118.                   oddfives = 0;
  119.                   ticklimit = 18;
  120.             }
  121.             else
  122.             {
  123.                   ticklimit = 17;
  124.             }
  125.    
  126.             /* Now, handle the seconds count */
  127.  
  128.             ++timestuff[14];
  129.  
  130.             if (timestuff[14] > '9')
  131.             {
  132.                   timestuff[14] = '0';
  133.  
  134.                   /* Now, handle the tens of seconds count */
  135.  
  136.                   ++timestuff[12];
  137.                   if (timestuff[12] > '5')
  138.                   {
  139.                         timestuff[12] = '0';
  140.  
  141.                         /* Now, handle the minutes count */
  142.  
  143.                         ++timestuff[8];
  144.                         if (timestuff[8] > '9')
  145.                         {
  146.                               timestuff[8] = '0';
  147.  
  148.                               /* Now, handle the ten minutes count */
  149.  
  150.                               ++timestuff[6];
  151.                               if (timestuff[6] > '5')
  152.                               {
  153.                                     timestuff[6] = '0';
  154.  
  155.                                     /* Now, handle the hours count */
  156.  
  157.                                     ++hours;
  158.                                     if(12 == hours)
  159.                                           if ('P' == timestuff[18])
  160.                                                 timestuff[18] = 'A';
  161.                                           else  timestuff[18] = 'P';
  162.  
  163.                                     if(hours > 12)
  164.                                           hours = 1;
  165.  
  166.                                     timestuff[0] = (hours > 9) ? '1' : ' ';
  167.                                     timestuff[2] = '0' + hours % 10;
  168.                               }
  169.                         }
  170.                   }
  171.             }
  172.  
  173.             /* Now, update the display */
  174.  
  175.             puttext(clockx, clocky, clockx+10, clocky, timestuff);
  176.       }
  177. }
  178.  
  179. void    far     stopclock(void)
  180. {
  181.       if(installed)
  182.       {
  183.             disable();
  184.             setvect(CLOCK, oldvector);
  185.             enable();
  186.             installed = FALSE;
  187.       }
  188. }
  189.  
  190. static  void    setstr(char *s, int ch, size_t n)
  191. {
  192.       size_t  i;
  193.     
  194.       for(i=0 ; i<n ; ++i)
  195.       {
  196.             s[i] = ch;
  197.       }
  198.  
  199.       s[i] = '\0';
  200. }
  201.